package com.tange.module.login.line;

import android.app.Activity;
import android.content.Intent;

import com.appbase.custom.constant.CommonConstants;
import com.linecorp.linesdk.LineApiResponse;
import com.linecorp.linesdk.api.LineApiClient;
import com.linecorp.linesdk.api.LineApiClientBuilder;
import com.tange.base.toolkit.PreferenceUtil;
import com.tg.appcommon.android.TGLog;
import com.tange.base.toolkit.TGThreadPool;

public class LineLoginProxy {

    private static final String TAG = "LineLoginProxy";

    public static void launch(Activity activity, int requestCode) {
        Intent intent = new Intent(activity, LineLoginActivity.class);
        activity.startActivityForResult(intent, requestCode);
    }

    public static void logout(Activity activity, Runnable runnable) {
        TGThreadPool.execute(() -> {
            try {
                LineApiClientBuilder apiClientBuilder = new LineApiClientBuilder(activity.getApplicationContext(), activity.getString(R.string.line_channel_id));
                LineApiClient lineApiClient = apiClientBuilder.build();
                LineApiResponse<?> response = lineApiClient.logout();

                TGLog.i(TAG, "[logout] response = " + response);

                PreferenceUtil.setString(activity, CommonConstants.LINE_ID_TOKEN, "");
                runnable.run();
            } catch (Throwable ex) {
                TGLog.i(TAG, "[logout] error : " + ex.getMessage());
            }
        });
    }
}
